//
//  CYStatusCellOriginalView.swift
//  CYSinaWB
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CYStatusCellOriginalView: UIView {
    //底部约束对象
    var bottomConstraint: Constraint?
    
    var status: CYStatus? {
        didSet {
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)
            nameLabel.text = status?.user?.name
            mbrank_img.image = status?.user?.mbrank_img
            //后边做
            
            textLabel.text = status?.text
            pictureView.imageUrls = status?.imageUrls
            print(status?.imageUrls)
            //根据数据中是否有配图视图 然后动态调整约束关系  涉及到cell的复用
            //status?.imageUrls 永远都不为空 如果没有配图视图会给定一个空数组
            //where : 可以获取到前面快速赋值不为空的对象
            //如果imageUrls 不为空 并且 imageUrls.count 不为0
            
            //解决约束冲突
            self.bottomConstraint?.uninstall()
            //1.先卸载老的约束对象  -> 使用属性 记录底部的约束对象
            //2.重新添加约束对象
            if let imageURLs = status?.imageUrls where imageURLs.count != 0 {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //有图
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
            }else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.brownColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        sepView.backgroundColor = UIColor.grayColor()
        addSubview(sepView)
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrank_img)
        addSubview(verified_type_img)
        addSubview(textLabel)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.height.equalTo(8)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
            make.top.equalTo(iconImageView.snp_top)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        mbrank_img.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        verified_type_img.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.right.equalTo(iconImageView.snp_right)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(textLabel.snp_left)
//            make.right.equalTo(textLabel.snp_right).offset(-statusCellMargin)
            make.top.equalTo(textLabel.snp_bottom).offset(statusCellMargin)
//            make.size.equalTo(<#T##other: CGFloat##CGFloat#>)
            //测试大小
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(textLabel.snp_bottom).offset(statusCellMargin)
       self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true
        
    }
    
    //头像
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //名称
    private lazy var nameLabel:UILabel = UILabel(text: "丶妮子", fontSize: 16, textColor: themeColor)
    //时间
    private lazy var timeLabel: UILabel = UILabel(text: "19:35", fontSize: 14, textColor: UIColor.lightGrayColor())
    //来源
    private lazy var sourceLabel: UILabel = UILabel(text: "macChen", fontSize: 14, textColor: UIColor.lightGrayColor())
    //等级
    private lazy var mbrank_img: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //认证
    private lazy var verified_type_img: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //内容
    private lazy var textLabel:UILabel = UILabel(text: "哎呀呀呀呀呀", fontSize: 16, textColor: UIColor.darkGrayColor(),alignment: .Left )
    
    private lazy var sepView: UIView = UIView()
    
    private lazy var pictureView: CYStatusPictureView = CYStatusPictureView()
    
}
